package com.wyz.saas.common.base

import android.os.Bundle
import com.atom.api.ApiContext
import com.atom.module.mvvm.base.IModel
import com.atom.module.mvvm.base.AbstractViewModel
import com.atom.module.mvvm.livedata.SingleLiveData
import com.wyz.saas.api.common.ContextApi
import java.util.HashMap

abstract class AbstractApiViewModel<M : AbstractApiModel> : AbstractViewModel<M>, ContextApi {

    constructor(id: Int, model: M?) : super(id, model)
    constructor(model: M?) : super(model)
    constructor() : super()

    var loadingAction: SingleLiveData<Boolean> = SingleLiveData()

    val startActivityAction = SingleLiveData<Map<String, Any>>()

    val finishAction = SingleLiveData<Unit>()

    val onBackPressedAction = SingleLiveData<Unit>()

    override fun apiContext(): ApiContext {
        return AbstractApiApplication.getInstance().apiContext()
    }

    fun startActivity(clz: Class<*>) {
        startActivity(clz, null)
    }

    fun startActivity(clz: Class<*>, bundle: Bundle?) {
        val params: MutableMap<String, Any> = HashMap()
        params[ParameterField.CLASS] = clz
        if (bundle != null) {
            params[ParameterField.BUNDLE] = bundle
        }
        startActivityAction.post(params)
    }

    fun finish() {
        finishAction.post(Unit)
    }

    fun onBackPressed() {
        onBackPressedAction.post(Unit)
    }

    object ParameterField {
        var CLASS = "CLASS"
        var CANONICAL_NAME = "CANONICAL_NAME"
        var BUNDLE = "BUNDLE"
    }
}